using System;
using System.IO;

namespace ADEVSO.IO
{
    public static class File
    {
        #region Methods

        /// <summary>
        /// Get Text File Lines
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static ADEVSO.Collections.ArrayList GetFileLines(String fileName)
        {
            ADEVSO.Collections.ArrayList lines = new ADEVSO.Collections.ArrayList();
            try
            {
                StreamReader reader = new StreamReader(fileName);
                try
                {
                    do
                    {
                        lines.Add(reader.ReadLine());
                    }
                    while (reader.Peek() != -1);
                }
                catch
                {
                    lines.Add("File is empty");
                }
                finally
                {
                    reader.Close();
                }
            }
            catch
            {
            }
            return lines;
        }

        /// <summary>
        /// Get Text File Lines
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static System.Text.StringBuilder LoadFile(String fileName)
        {
            System.Text.StringBuilder lines = new System.Text.StringBuilder();
            try
            {
                StreamReader reader = new StreamReader(fileName);
                try
                {
                    do
                    {
                        lines.AppendLine(reader.ReadLine());
                    }
                    while (reader.Peek() != -1);
                }
                catch
                {
                    lines.AppendLine("File is empty");
                }
                finally
                {
                    reader.Close();
                }
            }
            catch
            {
            }
            return lines;
        }

        /// <summary>
        /// Save Text Lines into File
        /// </summary>
        /// <param name="text"></param>
        /// <param name="fileName"></param>
        public static void SaveOnFile(ADEVSO.Collections.ArrayList text, String fileName)
        {
            try
            {
                StreamWriter writer = new StreamWriter(fileName);
                for (int i = 0; i < text.Count; i++)
                {
                    try
                    {
                        writer.WriteLine(text[i]);
                    }
                    catch { }
                }
                writer.Close();
            }
            catch
            {
            }
        }

        /// <summary>
        /// Save Text Lines into File
        /// </summary>
        /// <param name="text"></param>
        /// <param name="fileName"></param>
        public static void SaveOnFile(System.Text.StringBuilder text, String fileName)
        {
            try
            {
                String[] _text = text.ToString().Split(new String[] { Environment.NewLine }, StringSplitOptions.None);
                StreamWriter writer = new StreamWriter(fileName);
                for (int i = 0; i < _text.Length; i++)
                {
                    try
                    {
                        writer.WriteLine(_text[i]);
                    }
                    catch { }
                }
                writer.Close();
            }
            catch
            {
            }
        }

        /// <summary>
        /// Get File Info
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static ADEVSO.Collections.ArrayList GetFileInfo(String fileName)
        {
            ADEVSO.Collections.ArrayList report = new ADEVSO.Collections.ArrayList();
            try
            {
                FileInfo FileProps = new FileInfo(fileName);
                report.Add("File Name = " + FileProps.FullName);
                report.Add("Create Date = " + FileProps.CreationTime);
                report.Add("Last Accessed Date = " + FileProps.LastAccessTime);
                report.Add("Last Modified Date = " + FileProps.LastWriteTime);
                report.Add("Size = " + FileProps.Length);
                FileProps = null;
            }
            catch
            { }
            return report;
        }

        /// <summary>
        /// Get File Extension
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static String GetFileExtension(String fileName)
        {
            String temp = "", ext = ".";
            for (int i = fileName.Length - 1; i >= 0 && fileName[i] != '.'; i--)
                temp += fileName[i];
            temp = temp.ToLower();
            for (int i = temp.Length - 1; i >= 0; i--)
                ext += temp[i];
            return ext;
        }

        /// <summary>
        /// Verify is Extension is on Required Extensions
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="exts"></param>
        /// <param name="equals">True: Equals Extensions; False: Similar Extensions</param>
        /// <returns></returns>
        public static bool IsRequiredExtension(String fileName, ADEVSO.Collections.ArrayList exts, bool equals)
        {
            bool result = false;
            if (equals)
            {
                String ext = GetFileExtension(fileName);
                if (!exts.Contains(ext))
                    result = false;
            }
            else
            {
                for (int i = 0; i < exts.Count; i++)
                    if (fileName.EndsWith(exts[i].ToString()))
                        result = true;
            }
            return result;
        }

        /// <summary>
        /// Remove File Extension
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static String RemoveFileExtension(String fileName)
        {
            String temp = "";
            int i = -1;
            for (i = fileName.Length - 1; i >= 0 && fileName[i] != '.'; i--) ;
            for (int j = 0; j < i; j++)
                temp += fileName[j].ToString();
            return temp;
        }

        /// <summary>
        /// Replace File Extension
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="newExt">Include .</param>
        /// <returns></returns>
        public static String ReplaceFileExtension(String fileName, String newExt)
        {
            String temp = RemoveFileExtension(fileName);
            temp += newExt;
            return temp;
        }

        /// <summary>
        /// Rename File
        /// </summary>
        /// <param name="fileName">Complete File Name (with path)</param>
        /// <param name="newFileName">New File Name (without path)</param>
        /// <param name="overwrite">Overwrite a file if already exists a file with the new file name</param>
        public static void Rename(String fileName, String newFileName, bool overwrite)
        {
            if (System.IO.File.Exists(fileName))
            {
                if (! overwrite)
                {
                    if (System.IO.File.Exists(newFileName))
                    {
                        throw new Exception("Already exists a file with the new file name");
                    }
                }
                System.IO.File.Move(fileName, newFileName);
            }
            else
            {
                throw new Exception("The file don't exits");
            }
        }
        
        #endregion
    }    
}